package first.group.ssh.packet;

import first.group.ssh.SSHConsts;
import first.group.ssh.packet.reader.IntegerReader;

import java.nio.ByteBuffer;

public class ChannelFailurePacket extends Packet{
    public final int recipientChannel;

    public ChannelFailurePacket(int recipientChannel) {
        super(SSHConsts.SSH_MSG_CHANNEL_SUCCESS);
        this.recipientChannel = recipientChannel;
    }
    public static PacketParser<ChannelFailurePacket> PARSER = new ChannelFailurePacket.Parser();

    private static class Parser extends PacketParser<ChannelFailurePacket> {

        @Override
        public ChannelFailurePacket parse(ByteBuffer buffer) {
            var recipientChannel = IntegerReader.readInteger(buffer);
            if (recipientChannel == null) {
                return null;
            }
            return new ChannelFailurePacket(recipientChannel);
        }
    }

    @Override
    public void write(ByteBuffer buffer) {
        buffer.putInt(recipientChannel);
    }
}
